﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication.Models
{
    public class Nodes
    {
        private Models.DataClasses1DataContext data = new DataClasses1DataContext();
        private const int SendTimeout = 60;
        private string _id;
  
        public DateTime _registrationTime;
        public DateTime _lastSendTime;

        public Work Work;

        public int WorkID
        {
            get { return Work != null ? Work.id : -1; }
            set { Work = data.Work.First(t => t.id == value); }
        }
        public List<NodeWork> _nodeWorks; 

        public string Id
        {
           
            get { return _id; }
        }

        public Nodes()
        {
            _id =Guid.NewGuid().ToString();
            _registrationTime = DateTime.Now;
            _lastSendTime = DateTime.Now;
            _nodeWorks = new List<NodeWork>();

        }

   

        public bool Spare
        {
            get { return Work == null; }
        }


        public bool Activ
        {
            get { return (DateTime.Now - _lastSendTime).TotalSeconds < SendTimeout; }
        }

        public void Send()
        {
            _lastSendTime = DateTime.Now;
        }

        public string Run()
        {
            if (Work == null)
                return "true";

            Temp temp = null;
            var vv = data.Temp.Where(t => t.idWork == Work.id ).OrderByDescending(t => t.stap);

            if (vv.Any())
            {
                temp = vv.First();
            }
            NodeWork node = new NodeWork()
            {
                StartTime = DateTime.Now,
                Id = Guid.NewGuid().ToString(),
                Param = Work.param,
                IdDataSet = Work.idDataSet,
                IdWork = Work.id,
                StartStep = temp!=null?temp.stap +1: 0,
                EndSet =(temp!=null?temp.stap : 0)+ Convert.ToInt32(Work.param.Split(';')[5]),
               IdStartTemp= temp!=null ?temp.id: 0,
            };
            _nodeWorks.Add(node);
            return node.GetString();



        }

        public void NewResult(string workId, string value, byte[] filearray)
        {
            _lastSendTime = DateTime.Now;
            if (workId == _nodeWorks[_nodeWorks.Count - 1].Id)
            {
                _nodeWorks[_nodeWorks.Count - 1].EndTime = DateTime.Now;
                Temp temp = new Temp()
                {
                    data = filearray,
                    idWork = Work.id,
                    stap = _nodeWorks[_nodeWorks.Count - 1].EndSet,
                    test = value,
                    dateTime = DateTime.Now
                };
                data.Temp.InsertOnSubmit(temp);
                data.SubmitChanges();
                if (_nodeWorks[_nodeWorks.Count - 1].EndSet >= Convert.ToInt32(Work.param.Split(';')[4]))
                {   Work.state = 2;
                 
                    data.SubmitChanges();
                       Work = null;
}
            }
        }
    }

    public class NodeWork
    {

        public string Param;
        public string Id;
        public int IdWork;
        public int IdDataSet;
        public int StartStep;
        public int EndSet;
        public DateTime StartTime;
        public DateTime EndTime;
        public int IdStartTemp;
        public int IdSEndTemp;


     
        public string GetString()
        {
            return Id + "|" + Param + "|" + IdWork + "|" + IdDataSet + "|" + StartStep + "|" + EndSet +"|"+ IdStartTemp;
        }
    }
}